package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbOrderExample;
import com.pyg.pojo.TbOrderExample.Criteria;
import com.pyg.mapper.TbOrderMapper;
import com.pyg.pojo.TbOrder;
import com.pyg.goods.service.TbOrderService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbOrderServiceImpl implements TbOrderService {
	@Autowired
	private TbOrderMapper tbOrderMapper;

	@Override
	public List<TbOrder> findAll() {
		return tbOrderMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbOrder> page = (Page<TbOrder>) tbOrderMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbOrder
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbOrder tbOrder, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbOrderExample example = null;
		if (tbOrder != null) {
			example = new TbOrderExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbOrder.getOrderId())) {
				criteria.andOrderIdEqualTo(tbOrder.getOrderId() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getPayment())) {
				criteria.andPaymentEqualTo(tbOrder.getPayment() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getPaymentType())) {
				criteria.andPaymentTypeLike("%" + tbOrder.getPaymentType() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getPostFee())) {
				criteria.andPostFeeLike("%" + tbOrder.getPostFee() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getStatus())) {
				criteria.andStatusLike("%" + tbOrder.getStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getCreateTime())) {
				criteria.andCreateTimeEqualTo(tbOrder.getCreateTime() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getUpdateTime())) {
				criteria.andUpdateTimeEqualTo(tbOrder.getUpdateTime() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getPaymentTime())) {
				criteria.andPaymentTimeEqualTo(tbOrder.getPaymentTime() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getConsignTime())) {
				criteria.andConsignTimeEqualTo(tbOrder.getConsignTime() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getEndTime())) {
				criteria.andEndTimeEqualTo(tbOrder.getEndTime() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getCloseTime())) {
				criteria.andCloseTimeEqualTo(tbOrder.getCloseTime() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getShippingName())) {
				criteria.andShippingNameLike("%" + tbOrder.getShippingName() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getShippingCode())) {
				criteria.andShippingCodeLike("%" + tbOrder.getShippingCode() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getUserId())) {
				criteria.andUserIdLike("%" + tbOrder.getUserId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getBuyerMessage())) {
				criteria.andBuyerMessageLike("%" + tbOrder.getBuyerMessage() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getBuyerNick())) {
				criteria.andBuyerNickLike("%" + tbOrder.getBuyerNick() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getBuyerRate())) {
				criteria.andBuyerRateLike("%" + tbOrder.getBuyerRate() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getReceiverAreaName())) {
				criteria.andReceiverAreaNameLike("%" + tbOrder.getReceiverAreaName() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getReceiverMobile())) {
				criteria.andReceiverMobileLike("%" + tbOrder.getReceiverMobile() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getReceiverZipCode())) {
				criteria.andReceiverZipCodeLike("%" + tbOrder.getReceiverZipCode() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getReceiver())) {
				criteria.andReceiverLike("%" + tbOrder.getReceiver() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getExpire())) {
				criteria.andExpireEqualTo(tbOrder.getExpire() );
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getInvoiceType())) {
				criteria.andInvoiceTypeLike("%" + tbOrder.getInvoiceType() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getSourceType())) {
				criteria.andSourceTypeLike("%" + tbOrder.getSourceType() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrder.getSellerId())) {
				criteria.andSellerIdLike("%" + tbOrder.getSellerId() + "%");
			}
		}

		Page<TbOrder> page = (Page<TbOrder>) tbOrderMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbOrder tbOrder) {
		tbOrderMapper.insert(tbOrder);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbOrderMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbOrder findOne(Long id) {
		return tbOrderMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbOrder tbOrder) {
		tbOrderMapper.updateByPrimaryKey(tbOrder);
	}

}
